package com.useeinfo.oa.modules.task.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.enums.ResultEnums;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.DictInfoBiz;
import com.useeinfo.oa.modules.base.biz.ResourceBiz;
import com.useeinfo.oa.modules.base.entity.DictInfo;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Resource;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import com.useeinfo.oa.modules.task.dao.TaskWorkLogDao;
import com.useeinfo.oa.modules.task.entity.TaskOrder;
import com.useeinfo.oa.modules.task.entity.TaskWorkLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Set;

/**
 * @auther: bin.liu
 * @Date: 2019/7/5 15:24
 * @Description: 工作日志
 */
@Service("taskWorkLogBiz")
public class TaskWorkLogBiz extends CrudBiz<TaskWorkLogDao, TaskWorkLog> {

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private ResourceBiz resourceBiz;

    @Autowired
    private DictInfoBiz dictInfoBiz;

    @Autowired
    private TaskOrderBiz taskOrderBiz;


    public ResultDto taskWorkLogDetail(String taskWorkLogId){
        if(StringUtils.isBlank(taskWorkLogId)){
            return ResultUtil.error("参数错误");
        }
        TaskWorkLog taskWorkLog = dao.findModel(StringConverters.null2Long(taskWorkLogId));
        return ResultUtil.success(taskWorkLog.toJSONObject());
    }

    public ResultDto taskWorkLogList(String pageIndex,String pageSize,String taskOrderId){
        QueryParam queryParam = new QueryParam(pageIndex,pageSize);
        if(StringUtils.isNotBlank(taskOrderId)){
            queryParam.getSqlMap().put("taskOrderId",taskOrderId);
        }
        return ResultUtil.success(findJSONList(queryParam));
    }

    public ResultDto addLog(String logId, String workLogCatId, String workLogLevelId, String workContent, String photoIds,String taskOrderId) {
        Enterprise sessionEnterprise = SessionKeyUtils.getSessionEnterprise(httpServletRequest);
        SystemUser systemUser = SessionKeyUtils.getSessionUser(httpServletRequest);
        if (systemUser == null || sessionEnterprise == null) {
            return ResultUtil.error(ResultEnums.NO_LOGIN_MESSAGE);
        }
        TaskWorkLog taskWorkLog;
        if (StringUtils.isNoneBlank(logId)) {
            taskWorkLog = dao.findModel(StringConverters.null2Long(logId));
        } else {
            taskWorkLog = new TaskWorkLog();
            taskWorkLog.setCode(dao.getCode(StringConverters.null2String(sessionEnterprise.getId())));
        }

        taskWorkLog.setSystemUser(systemUser);
        taskWorkLog.setWorkContent(StringConverters.null2String(workContent));

        if (StringUtils.isNotBlank(photoIds)) {
            String[] photoIdsArr = photoIds.split(",");
            Set<Resource> resources = new HashSet<>();
            for (String id : photoIdsArr) {
                Resource resource = resourceBiz.findModel(StringConverters.null2Long(id));
                resources.add(resource);
            }
            taskWorkLog.setLogPhoto(resources);
        }

        if (StringUtils.isNotBlank(workLogCatId)) {
            DictInfo workLogCat = dictInfoBiz.findModel(StringConverters.null2Long(workLogCatId));
            taskWorkLog.setWorkLogCat(workLogCat);
        }

        if (StringUtils.isNotBlank(workLogLevelId)) {
            DictInfo workLogLevel = dictInfoBiz.findModel(StringConverters.null2Long(workLogLevelId));
            taskWorkLog.setWorkLogLevel(workLogLevel);
        }

        if(StringUtils.isNotBlank(taskOrderId)){
            TaskOrder taskOrder = taskOrderBiz.findModel(StringConverters.null2Long(taskOrderId));
            taskWorkLog.setTaskOrder(taskOrder);
        }
        addOrUpdate(taskWorkLog);

        return ResultUtil.success();
    }
}
